package com.wuliming.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wuliming.gulimall.product.entity.ProductAttrValueEntity;
import com.wuliming.gulimall.product.entity.vo.req.AttrReqVo;
import com.wuliming.gulimall.product.entity.vo.resp.AttrRespVo;
import com.wuliming.gulimall.product.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wuliming.gulimall.product.service.AttrService;
import com.wuliming.common.utils.PageUtils;
import com.wuliming.common.utils.R;


/**
 * 商品属性
 *
 * @author wuliming
 * @email wuliming@gmail.com
 * @date 2023-12-05 12:11:06
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId,
                  @PathVariable("type") String type) {
        PageUtils page = attrService.queryListPage(params, catelogId,type);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> data = attrService.listforspu(spuId);

        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo respVo = attrService.getByIdInfo(attrId);
        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrReqVo attr) {
//		attrService.save(attr);
        attrService.saveDetail(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrReqVo attr) {
        attrService.updateDetail(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update/{spuId}")
    public R update(@PathVariable("spuId") Long spuId,
    @RequestBody List<ProductAttrValueEntity> entityList) {
        productAttrValueService.updateSpuId(spuId,entityList);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
